package ${pkg}.controller;


import ${pkg}.entity.*;
import ${pkg}.model.vo.${sc(entityName)}.*;
import ${pkg}.dbservice.*;
import com.boe.common.annotation.*;
import com.boe.common.mvc.*;
import lombok.*;
import java.time.*;
import java.util.*;
import com.boe.common.util.*;
import org.springframework.beans.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;

/**
 * ${entityCn}
 * Created by ${user} at ${now}
 */
@RestController
@RequestMapping("/${kb(group)}/${kb(entityName)}")
@RequiredArgsConstructor
public class ${bc(entityName)}Controller {

    @NonNull
    private I${bc(entityName)}Service ${sc(entityName)}Service;


    @PostMapping("/list")
    public ListPageResult<${bc(entityName)}ResponseVO> list(@RequestBody ListPageRequest<${bc(entityName)}RequestVO> requestParam) {
        var ret = ${sc(entityName)}Service.list(requestParam.getQueryBody(), requestParam.getPageNo(), requestParam.getPageSize(), requestParam.getOrder());

        return ret;
    }

    @PostMapping("/detail/{id}")
    public ApiResult<${bc(entityName)}> detail(@PathVariable String id) {
        var ret = ${sc(entityName)}Service.getById(id);
        return ApiResult.of(ret);
    }

    @PostMapping("/add")
    public ApiResult<${bc(entityName)}> add(@RequestBody @Validated ${bc(entityName)}AddVO ${sc(entityName)}AddVO) {
        var entity = new ${bc(entityName)}();
        BeanUtils.copyProperties(${sc(entityName)}AddVO, entity);

        if (${sc(entityName)}Service.save(entity) == false) {
            return ApiResult.error("添加失败!");
        }

        return ApiResult.of(entity);
    }


    @PostMapping("/update/{id}")
    public ApiResult<String> update(@PathVariable String id, @RequestBody @Validated ${bc(entityName)}UpdateVO ${sc(entityName)}UpdateVO) {

        var ${sc(entityName)} = new ${bc(entityName)}();
        BeanUtils.copyProperties(${sc(entityName)}UpdateVO, ${sc(entityName)});

        if (${sc(entityName)}Service.update(id, ${sc(entityName)}) == false) {
            return ApiResult.error("更新失败!");
        }

        return new ApiResult();
    }


    @PostMapping("/delete/{id}")
    public ApiResult<String> delete(@PathVariable String id) {

        if (${sc(entityName)}Service.removeById(id) == false) {
            return ApiResult.error("删除失败!");
        }

        return new ApiResult();
    }
}